package com.jwsoft.manager.core.task.zjjy;

import com.alibaba.fastjson.JSON;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.cache.redis.RedisHelper;
import com.jwsoft.manager.common.enums.EnrollStatusFormalEnum;
import com.jwsoft.manager.common.vo.common.recruitStudent.SyncStudentQueryVO;
import com.jwsoft.manager.core.integration.EduSyncZjApproveIntegration;
import com.jwsoft.manager.core.integration.EduSyncZjEnrollIntegration;
import com.jwsoft.manager.core.integration.impl.EduSyncZjEnrollIntegrationImpl;
import com.jwsoft.manager.core.util.EduHelper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 省招生录取库同步数据生成
 *
 * @author chenzh
 */
@Component
@Slf4j
public class CreateLqTaskJob {
    @Autowired
    EduSyncZjEnrollIntegrationImpl eduSyncZjEnrollIntegration;
    @Autowired
    private RedisHelper redisHelper;
    @Autowired
    private EduHelper eduHelper;
    @XxlJob("createZjjyLqData")
    public void jhjyBmJobHandler() {
        XxlJobHelper.log("XXL-JOB, createZjjyLqData");
        String str = XxlJobHelper.getJobParam();
        SyncStudentQueryVO vo = JSON.parseObject(str, SyncStudentQueryVO.class);
        String redisKey = "edu:zjSync:create:enroll";
        if (redisHelper.hasKey(redisKey)) {
            throw new AppException("正在执行中");
        }
        redisHelper.set(redisKey, true, 600);
        vo.setYear(eduHelper.thisYear());
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取200条
            vo.setPageNum(1);
            vo.setPageSize(100);
        }
        if (StringUtils.isBlank(vo.getAreaCode())) {
            vo.setAreaCode(eduHelper.thisAreaCode());
        }
        if (StringUtils.isBlank(vo.getEnrollStatus())) {
            vo.setEnrollStatus(EnrollStatusFormalEnum.lq.getType());
        }
        log.info("省招生录取库同步数据生成开始");
        try {
            eduSyncZjEnrollIntegration.createDo(vo);
        } catch (Exception e) {
            redisHelper.del(redisKey);
            e.printStackTrace();
        }
        log.info("省招生录取库同步数据生成结束");
    }
}
